package com.example.demo.util;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Objects;

public class UrlCompressUtils {

    private static final String DES = "DES";
    private static final int keysizeDES = 56;
    private static final String KEY = "9de1cdaa-0bea-4d1d-935c-af9559d2dd76";

    public UrlCompressUtils() {
    }

    public static String dESencode(String res) {
        return keyGeneratorES(res, true);
    }

    public static String dESdecode(String res) {
        return keyGeneratorES(res, false);
    }

    private static String parseByte2HexStr(byte[] buf) {
        StringBuilder sb = new StringBuilder();
        for (byte b : buf) {
            String hex = Integer.toHexString(b & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    private static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }

            return result;
        }
    }

    @SuppressWarnings("unused")
	private static String keyGeneratorES(String res, boolean isEncode) {
        try {
            KeyGenerator kg = KeyGenerator.getInstance(DES);
            String charset = "utf-8";
            if (UrlCompressUtils.keysizeDES == 0) {
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG") ;
                secureRandom.setSeed(UrlCompressUtils.KEY.getBytes(charset));
                kg.init(UrlCompressUtils.keysizeDES, secureRandom);
            } else {
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG") ;
                secureRandom.setSeed(UrlCompressUtils.KEY.getBytes(charset));
                kg.init(UrlCompressUtils.keysizeDES, secureRandom);
            }

            SecretKey sk = kg.generateKey();
            SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), "DES");
            Cipher cipher = Cipher.getInstance("DES");
            if (isEncode) {
                cipher.init(1, sks);
                byte[] resBytes = res.getBytes(charset);
                return parseByte2HexStr(cipher.doFinal(resBytes));
            } else {
                cipher.init(2, sks);
                return new String(cipher.doFinal(Objects.requireNonNull(parseHexStr2Byte(res))));
            }
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String url = "www.baidu.com";
        String encode = UrlCompressUtils.dESencode(url);
        System.out.println(encode);

        String sdecode = UrlCompressUtils.dESdecode(encode);
        System.out.println(sdecode);
    }
}
